<?php
namespace app\admin\model;
use think\Db;
use app\admin\model\Common;
use think\Session;
use think\Validate;

class  Report extends Common
{
     /*
    分为三块来写，辅导员操作，高校老师操作，以及教育厅操作
	 */
	
	//---------------辅导员操作------------------
	public function Text($param){
	}
	
	//下拉列表
	public function ListDropdowm()
	{

    $data=Db::table('oa_list')->select();
    $parent = [];
    $child = [];
    $support = [];
    $achievements = [];
    foreach ($data as $key => $value) {
      switch ($value['name']) {
        case 'type':
          if ($value['parent'] == 0) {
            array_push($parent, $value);
          } else {
            array_push($child, $value);
          }
          break;
        case 'support':
          array_push($support, $value);
          break;
        case 'achievements':
          array_push($achievements, $value);
          break;
        default:
          break;
      }
    }
    foreach ($parent as $k1 => $v1) {
         foreach($child as $k2 => $v2){
          if($parent[$k1]['id']==$child[$k2]['parent'])
            $parent[$k1]['children'][]=$child[$k2];
         }
    }
		$res['type']=$parent;
		$res['support']=$support;
		$res['achievements']=$achievements;
		return $res;
	}

	//添加子项目
	public function WriteTrain($param)
	{   
		
			 $idnumber=$param['username'];
			 $train_id1=Db::table('oa_admin_train')
								 ->where('idnumber',$idnumber)
								 ->field('id')
								 ->find();
				$train_id=$train_id1['id'];
				$result1=$this->istrain($train_id);
			 if($result1['code']==false)
			 {
				 $this->error=$result1['error'];
				 return false;
			 }
			 $partner=$param['partner'];
			 $result2=$this->ispartner($partner);
			 if($result2['code']==false)
			 {
				 $this->error=$result2['error'];
				 return false;
			 }


		$infoa['book_fee']=$param['form']['book_fee'];
		$infoa['book_fee_basis']=$param['form']['book_fee_basis'];
		$infoa['data_fee']=$param['form']['data_fee'];
		$infoa['data_fee_basis']=$param['form']['data_fee_basis'];
		$infoa['travel_fee']=$param['form']['travel_fee'];
		$infoa['travel_fee_basis']=$param['form']['travel_fee_basis'];
		$infoa['meeting_fee']=$param['form']['meeting_fee'];
		$infoa['meeting_fee_basis']=$param['form']['meeting_fee_basis'];
		$infoa['international_fee']=$param['form']['international_fee'];
		$infoa['international_fee_basis']=$param['form']['international_fee_basis'];
		$infoa['equipment_fee']=$param['form']['equipment_fee'];
		$infoa['equipment_fee_basis']=$param['form']['equipment_fee_basis'];
		$infoa['consulting_fee']=$param['form']['consulting_fee'];
		$infoa['consulting_fee_basis']=$param['form']['consulting_fee_basis'];
		$infoa['labour_fee']=$param['form']['labour_fee'];
		$infoa['labour_fee_basis']=$param['form']['labour_fee_basis'];
		$infoa['print_fee']=$param['form']['print_fee'];
		$infoa['print_fee_basis']=$param['form']['print_fee_basis'];
		$infoa['administration_fee']=$param['form']['administration_fee'];
		$infoa['administration_fee_basis']=$param['form']['administration_fee_basis'];
		$infoa['other_fee']=$param['form']['other_fee'];
		$infoa['other_fee_basis']=$param['form']['other_fee_basis'];
		$infoa['all_fee']=$param['form']['book_fee']+$param['form']['data_fee']+$param['form']['travel_fee']+$param['form']['meeting_fee']+$param['form']['international_fee']+$param['form']['equipment_fee']+$param['form']['consulting_fee']+$param['form']['labour_fee']+$param['form']['print_fee']+$param['form']['administration_fee']+$param['form']['other_fee'];
		$infoa['status']=1;
		$fund_id=Db::table('oa_report_fund')
						->insertGetId($infoa);


			 $info['fund_id']=(int)$fund_id;
		$info['project_id']=$param['project_id'];
		$info['train_id']=$train_id;
		$info['title']=$param['form']['title'];
		$info['theme']=$param['form']['theme'];
		$info['research_direction']=$param['form']['research_direction'];
		$info['unit']=$param['form']['unit'];
		$info['result']=$param['result'][0];
		$info['word_count']=$param['form']['word_count'];
		$info['finish_time']=$param['form']['finish_time'];
		$info['achievement']=$param['achievement'];
		$info['postal_address']=$param['form']['postal_address'];
		$info['post_code']=$param['form']['post_code'];
		$info['status']=1;
		$info['type']=1;
		$subproject_id=Db::table('oa_report_subproject')
						->insertGetId($info);
		$infob['subproject_id']=(int)$subproject_id;

		for($i=0;$i<count($param['partner']);$i++)
		{
			$infob['name']=$param['partner'][$i]['name'];
			$infob['idnumber']=$param['partner'][$i]['idnumber'];
				 $a=substr($infob['idnumber'],6,4);
				 $b=substr($infob['idnumber'],10,2);
				 $c=substr($infob['idnumber'],12,2);
				 $infob['birth']=$a.'-'.$b.'-'.$c;
			$infob['sex']=$param['partner'][$i]['sex'];
			$infob['jobrank']=$param['partner'][$i]['jobrank'];
			$infob['task']=$param['partner'][$i]['task'];
			$infob['expertise']=$param['partner'][$i]['expertise'];
			$infob['edu']=$param['partner'][$i]['edu'];
			$infob['degree']=$param['partner'][$i]['degree'];
			$infob['unit']=$param['partner'][$i]['unit'];
			Db::table('oa_report_partner')
							->insert($infob);
		}
		return true;

	}
	 //修改子项目
	public function ReviseTrain($param)
	{
		$partner=$param['partner'];
		$subproject_id=$param['subproject']['id'];

		Db::table('oa_report_partner')
					 ->where('subproject_id',$subproject_id)
					 ->update([
						 'is_del'=>'2',
					 ]);
		$result2=$this->ispartner($partner);
				if($result2['code']==false)
				{
					$this->error=$result2['error'];
					return false;
				}
		 $id=$param['subproject']['id'];
	    $fund_id=$param['subproject']['fund_id'];
		$infoa['book_fee']=$param['fund']['book_fee'];
	 	$infoa['book_fee_basis']=$param['fund']['book_fee_basis'];
	 	$infoa['data_fee']=$param['fund']['data_fee'];
	 	$infoa['data_fee_basis']=$param['fund']['data_fee_basis'];
	 	$infoa['travel_fee']=$param['fund']['travel_fee'];
	 	$infoa['travel_fee_basis']=$param['fund']['travel_fee_basis'];
	 	$infoa['meeting_fee']=$param['fund']['meeting_fee'];
	 	$infoa['meeting_fee_basis']=$param['fund']['meeting_fee_basis'];
	 	$infoa['international_fee']=$param['fund']['international_fee'];
	 	$infoa['international_fee_basis']=$param['fund']['international_fee_basis'];
	 	$infoa['equipment_fee']=$param['fund']['equipment_fee'];
	 	$infoa['equipment_fee_basis']=$param['fund']['equipment_fee_basis'];
	 	$infoa['consulting_fee']=$param['fund']['consulting_fee'];
	 	$infoa['consulting_fee_basis']=$param['fund']['consulting_fee_basis'];
	 	$infoa['labour_fee']=$param['fund']['labour_fee'];
	 	$infoa['labour_fee_basis']=$param['fund']['labour_fee_basis'];
	 	$infoa['print_fee']=$param['fund']['print_fee'];
	 	$infoa['print_fee_basis']=$param['fund']['print_fee_basis'];
	 	$infoa['administration_fee']=$param['fund']['administration_fee'];
	 	$infoa['administration_fee_basis']=$param['fund']['administration_fee_basis'];
	 	$infoa['other_fee']=$param['fund']['other_fee'];
	 	$infoa['other_fee_basis']=$param['fund']['other_fee_basis'];
	 	$infoa['all_fee']=$param['fund']['book_fee']+$param['fund']['data_fee']+$param['fund']['travel_fee']+$param['fund']['meeting_fee']+$param['fund']['international_fee']+$param['fund']['equipment_fee']+$param['fund']['consulting_fee']+$param['fund']['labour_fee']+$param['fund']['print_fee']+$param['fund']['administration_fee']+$param['fund']['other_fee'];
	 	 Db::table('oa_report_fund')
	 	  ->where('id',$fund_id)
          ->update([
            'book_fee'=> $infoa['book_fee'],
            'book_fee_basis'=> $infoa['book_fee_basis'],
            'data_fee'=> $infoa['data_fee'], 
            'data_fee_basis'=> $infoa['data_fee_basis'], 
            'travel_fee'=>$infoa['travel_fee'],
            'travel_fee_basis'=>$infoa['travel_fee_basis'],
            'meeting_fee'=>$infoa['meeting_fee'],
            'meeting_fee_basis'=>$infoa['meeting_fee_basis'],
            'international_fee'=>$infoa['international_fee'],
            'international_fee_basis'=>$infoa['international_fee_basis'],
            'equipment_fee'=> $infoa['equipment_fee'],
            'equipment_fee_basis'=>$infoa['equipment_fee_basis'],
            'consulting_fee'=>$infoa['consulting_fee'],
            'consulting_fee_basis'=>$infoa['consulting_fee_basis'],
            'labour_fee'=>$infoa['labour_fee'],
            'labour_fee_basis'=>$infoa['labour_fee_basis'],
            'print_fee'=>$infoa['print_fee'],
            'print_fee_basis'=>$infoa['print_fee_basis'],
            'administration_fee'=>$infoa['administration_fee'],
            'administration_fee_basis'=>$infoa['administration_fee_basis'],
            'other_fee'=>$infoa['other_fee'],
            'other_fee_basis'=>$infoa['other_fee_basis'],
            'all_fee'=>$infoa['all_fee'],
            ]);

	 	$info['title']=$param['subproject']['title'];
	 	$info['theme']=$param['subproject']['theme'];
	 	$info['research_direction']=$param['subproject']['research_direction'];
	 	$info['unit']=$param['subproject']['unit'];
	 	$info['result']=$param['subproject']['result'][0];
	 	$info['word_count']=$param['subproject']['word_count'];
	 	$info['finish_time']=$param['subproject']['finish_time'];
	 	$info['achievement']=$param['subproject']['achievement'];
	 	$info['postal_address']=$param['subproject']['postal_address'];
	 	$info['post_code']=$param['subproject']['post_code'];
          Db::table('oa_report_subproject')
          ->where('id',$id)
          ->update([
            'title'=> $info['title'],
            'theme'=> $info['theme'], 
            'research_direction'=>$info['research_direction'],
            'unit'=>$info['unit'],
            'result'=>$info['result'],
            'word_count'=>$info['word_count'],
            'finish_time'=>$info['finish_time'],
            'achievement'=> $info['achievement'],
            'postal_address'=> $info['postal_address'],
            'post_code'=> $info['post_code'],
          ]);

         for($i=0;$i<count($param['partner']);$i++)
	 	{   $infob['id']=$param['partner'][$i]['id'];
	 		$infob['name']=$param['partner'][$i]['name'];
	 		$infob['sex']=$param['partner'][$i]['sex'];
			$infob['idnumber']=$param['partner'][$i]['idnumber'];
					$a=substr($infob['idnumber'],6,4);
					$b=substr($infob['idnumber'],10,2);
					$c=substr($infob['idnumber'],12,2);
					$infob['birth']=$a.'-'.$b.'-'.$c;
			$infob['task']=$param['partner'][$i]['task'];
	 		$infob['jobrank']=$param['partner'][$i]['jobrank'];
	 		$infob['expertise']=$param['partner'][$i]['expertise'];
	 		$infob['edu']=$param['partner'][$i]['edu'];
	 		$infob['degree']=$param['partner'][$i]['degree'];
	 		$infob['unit']=$param['partner'][$i]['unit'];
			$infob['subproject_id']=$subproject_id;
			 Db::table('oa_report_partner')
					 ->insert($infob);
	 	}
	 	return true;
	}


	//查看子项目详情
	public function ListChildren($param)
    {
    	$subproject_id=$param['id'];
    	$subproject=Db::table('oa_report_subproject')
	 		->where('id',$subproject_id)
	 		->select();
	    $data['subproject']=$subproject;
	    $fund_id=$subproject[0]['fund_id'];
	    $fund=Db::table('oa_report_fund')
	 		->where('id',$fund_id)
	 		->select();
	    $data['fund']=$fund;
	    $partner=Db::table('oa_report_partner')
			 ->where('subproject_id',$subproject_id)
			 ->where('is_del',1)
	 		->select();
	    $train=Db::table('oa_admin_train')
	 		->where('id',(int)$subproject[0]['train_id'])
			 ->select();
		$train_id=(int)$train[0]['id'];
		$time=strtotime("-5 year");
		$time1=date("Y-m-d",$time);
		$exachievement=Db::table('oa_admin_exachievement')
					  ->where('train_id',$train_id)
					  ->where('publish_time','> time',$time1)
					  ->limit(5)
					  ->select();
		$data['exachievement']=$exachievement;
	 	$data['train']=$train;
	 	$data['partner']=$partner;
     return $data;
    }
    //查看子项目的提交，中期审核，结题
    public function ListReport($param){
		$map=[];
		$status=$param['search_status'];
		$sch=$param['search_sch'];
		$name=$param['search_name'];
		$title=$param['search_title'];
		$type=(int)$param['type'];
		if(isset($status)&&$status!='')
		{
		  if((int)$status==10&&$type==1){
			$map['s.status']=['between',[10,23]];
		  }else if((int)$status==15&&$type==2){
			$map['s.status']=['between',[15,23]];  
		  }else{
			$map['s.status']=['between',[(int)$status,(int)$status]];
		  }
		}
		if(isset($name)&&$name!=''){
			$map['t.name']=['like', '%'.$name.'%'];
		}
		if(isset($title)&&$title!=''){
			$map['s.title']=['like', '%'.$title.'%'];
		}
		if(isset($sch)&&$sch!=''){
			$map['t.sch']=['like', '%'.$sch.'%'];
		}		
		if($type==1){	
			$type='0,23';
		}else if($type==2){
			$type='11,23';
		}else{
			$type='16,23';
		}
    	$level=$param['level'];
		$project_id=$param['project_id'];
    	if($level==1){
    		$idnumber=$param['idnumber'];
    		$id=Db::table('oa_admin_train')
	 		->where('idnumber',$idnumber)
	 		->field('id')
	 		->find();
	 		$train_id=$id['id'];
			$subproject=Db::table('oa_report_subproject')
						->alias('s')
						->join('oa_admin_train t','t.id=s.train_id')
						->where('s.train_id',$train_id)
						->where($map)
						->where('s.status','between',$type)
						->where('s.project_id',$project_id)
						->field('s.id,s.title,t.name,t.jobrank,t.sch,s.status')
				 		->paginate(config('pagesize'), false, ['page' => $page]);

    	}else if($level==2){
    		$sch=$param['sch'];	 		
    		$id=Db::table('oa_admin_train')
	 		->where('sch',$sch)
	 		->field('id')
	 		->select();
	 		for($i=0;$i<count($id);$i++)
	 		{
	 			$train_id[$i]=$id[$i]['id'];
	 		}
	 		$subproject=Db::table('oa_report_subproject')
						->alias('s')
						->join('oa_admin_train t','t.id=s.train_id')
						->where('s.train_id',"in",$train_id)
						->where('s.status','between',$type)
						->where($map)
						->where('s.project_id',$project_id)
						->where('s.status','not in',[3,5,7,9])
						->field('s.id,s.title,t.name,t.jobrank,t.sch,s.status')
				 		->paginate(config('pagesize'), false, ['page' => $page]);

    	}else if($level==3){
    		$subproject=Db::table('oa_report_subproject')
						->alias('s')
						->join('oa_admin_train t','t.id=s.train_id')
						->where('s.project_id',$project_id)
						->where($map)
						->where('s.status','between',$type)
						->where('s.status','not in',[3,5,7,9])
						->field('s.id,s.title,t.name,t.jobrank,t.sch,s.status')
	 		            ->paginate(config('pagesize'), false, ['page' => $page]);
    	}
    	return $subproject;
	}
	
    //中期审核录入
//  public function WriteMid($param)
//  {

// 	 $infob['filling_date']=$param['form'][0]['data'];
// 	 $infob['research_progress']=$param['Process_form'][0]['research_progress'];
// 	 $infob['funds']=$param['plan_form'][0]['funds'];
// 	 $infob['next_plan']=$param['plan_form'][0]['next_plan'];
// 	 $infob['funding_gap']=$param['plan_form'][0]['funding_gap'];
// 	 $infob['responsible_unit']=$param['form'][0]['responsible_unit'];

// 	 $info['book_fee']=$param['plan_form'][0]['book_fee'];
// 	 $info['data_fee']=$param['plan_form'][0]['data_fee'];
// 	 $info['travel_fee']=$param['plan_form'][0]['travel_fee'];
// 	 $info['meeting_fee']=$param['plan_form'][0]['meeting_fee'];
// 	 $info['international_fee']=$param['plan_form'][0]['international_fee'];
// 	 $info['equipment_fee']=$param['plan_form'][0]['equipment_fee'];
// 	 $info['labour_fee']=$param['plan_form'][0]['labour_fee'];
// 	 $info['print_fee']=$param['plan_form'][0]['print_fee'];
// 	 $info['administration_fee']=$param['plan_form'][0]['administration_fee'];
// 	 $info['other_fee']=$param['plan_form'][0]['other_fee'];
// 	 $info['all_fee']=$param['plan_form'][0]['all_fee'];
// 	 $info['consulting_fee']=$param['plan_form'][0]['consulting_fee'];
// 	 $info['surplus']=$param['plan_form'][0]['surplus'];
// 	 $info['status']=2;
// 	 $exfund_id=Db::table('oa_report_fund')
// 		   ->insertGetId($info);
 	        

// 	 $infoc['book_fee']=$param['plan_form'][0]['book_fee_budget'];
// 	 $infoc['data_fee']=$param['plan_form'][0]['data_fee_budget'];
// 	 $infoc['travel_fee']=$param['plan_form'][0]['travel_fee_budget'];
// 	 $infoc['meeting_fee']=$param['plan_form'][0]['meeting_fee_budget'];
// 	 $infoc['international_fee']=$param['plan_form'][0]['international_fee_budget'];
// 	 $infoc['equipment_fee']=$param['plan_form'][0]['equipment_fee_budget'];
// 	 $infoc['labour_fee']=$param['plan_form'][0]['labour_fee_budget'];
// 	 $infoc['print_fee']=$param['plan_form'][0]['print_fee_budget'];
// 	 $infoc['administration_fee']=$param['plan_form'][0]['administration_fee_budget'];
// 	 $infoc['other_fee']=$param['plan_form'][0]['other_fee_budget'];
// 	 $infoc['all_fee']=$param['plan_form'][0]['all_fee_budget'];
// 	 $infoc['consulting_fee']=$param['plan_form'][0]['consulting_fee_budget'];
// 	 $infoc['status']=3;
// 	 $unfund_id=Db::table('oa_report_fund')
// 			->insert($infoc);

// 	  $infob['unfund_id']=(int)$unfund_id;
// 	  $infob['exfund_id']=(int)$exfund_id;
// 		 $mid_id=Db::table('oa_report_project_mid')
// 				->insert($infob);
// 	  $infod['mid_id']=(int)$mid_id;
	 
// 	  for($i=0;$i<count($param['achievement_form']);$i++)
// 	  {
		 
// 		 $infod['achievement_name']=$param['achievement_form'][$i]['achievement_name'];
// 		 $infod['achievement_author']=$param['achievement_form'][$i]['achievement_author'];
// 		 $infod['achievement_type']=$param['achievement_form'][$i]['achievement_type'];
// 		 $infod['achievement_publish']=$param['achievement_form'][$i]['achievement_publish'];
// 		 $infod['achievement_publish_time']=$param['achievement_form'][$i]['achievement_publish_time'];
// 		 $infod['achievement_number']=$param['achievement_form'][$i]['achievement_number'];
// 		 $infod['achievement']=$param['achievement_form'][$i]['achievement'];
// 			 Db::table('oa_report_achievement')
// 					 ->insert($infod);
// 	  }
// 	  return true;

//  }
 //中期审核修改
 public function WriteMid($param)
 {
	 if(isset($param['delete'])){
		 Db::table('oa_report_achievement')
		 ->where('id',"in",$param['delete'])
		 ->delete();
	 }
	 $mid_id=$param['form'][0]['project_mid_id'];
	 $unfund_id=$param['plan_form'][0]['unfund_id'];
	 $exfund_id=$param['plan_form'][0]['exfund_id'];
	 $achivevment_id=$param['achivevment_form'][0]['id'];
	 $infob['filling_date']=$param['form'][0]['data'];
	 $infob['research_progress']=$param['Process_form'][0]['research_progress'];
	 $infob['funds']=$param['plan_form'][0]['funds'];
	 $infob['next_plan']=$param['plan_form'][0]['next_plan'];
	 $infob['funding_gap']=$param['plan_form'][0]['funding_gap'];
	 $infob['responsible_unit']=$param['form'][0]['responsible_unit'];

	 $info['book_fee']=$param['plan_form'][0]['book_fee'];
	 $info['data_fee']=$param['plan_form'][0]['data_fee'];
	 $info['travel_fee']=$param['plan_form'][0]['travel_fee'];
	 $info['meeting_fee']=$param['plan_form'][0]['meeting_fee'];
	 $info['international_fee']=$param['plan_form'][0]['international_fee'];
	 $info['equipment_fee']=$param['plan_form'][0]['equipment_fee'];
	 $info['labour_fee']=$param['plan_form'][0]['labour_fee'];
	 $info['print_fee']=$param['plan_form'][0]['print_fee'];
	 $info['administration_fee']=$param['plan_form'][0]['administration_fee'];
	 $info['other_fee']=$param['plan_form'][0]['other_fee'];
	 $info['all_fee']=$param['plan_form'][0]['all_fee'];
	 $info['consulting_fee']=$param['plan_form'][0]['consulting_fee'];
	 $info['surplus']=$param['plan_form'][0]['surplus'];
	 $info['status']=2;
	 $res1=Db::table('oa_report_fund')
			 ->where('id',$exfund_id)
			 ->update([
				 'book_fee'=>$info['book_fee'], 
				 'data_fee'=>$info['data_fee'],
				 'travel_fee'=>$info['travel_fee'],
				 'meeting_fee'=>$info['meeting_fee'],
				 'international_fee'=>$info['international_fee'],
				 'equipment_fee'=>$info['equipment_fee'],
				 'labour_fee'=>$info['labour_fee'], 
				 'print_fee'=>$info['print_fee'],
				 'administration_fee'=>$info['administration_fee'],
				 'other_fee'=>$info['other_fee'],
				 'all_fee'=>$info['all_fee'],
				 'consulting_fee'=>$info['consulting_fee'],
				 'surplus'=>$info['surplus'], 
			  ]);	 	        
	 $infoc['book_fee']=$param['plan_form'][0]['book_fee_budget'];
	 $infoc['data_fee']=$param['plan_form'][0]['data_fee_budget'];
	 $infoc['travel_fee']=$param['plan_form'][0]['travel_fee_budget'];
	 $infoc['meeting_fee']=$param['plan_form'][0]['meeting_fee_budget'];
	 $infoc['international_fee']=$param['plan_form'][0]['international_fee_budget'];
	 $infoc['equipment_fee']=$param['plan_form'][0]['equipment_fee_budget'];
	 $infoc['labour_fee']=$param['plan_form'][0]['labour_fee_budget'];
	 $infoc['print_fee']=$param['plan_form'][0]['print_fee_budget'];
	 $infoc['administration_fee']=$param['plan_form'][0]['administration_fee_budget'];
	 $infoc['other_fee']=$param['plan_form'][0]['other_fee_budget'];
	 $infoc['all_fee']=$param['plan_form'][0]['all_fee_budget'];
	 $infoc['consulting_fee']=$param['plan_form'][0]['consulting_fee_budget'];
	 $infoc['status']=3;
	 $res2=Db::table('oa_report_fund')
				 ->where('id',$unfund_id)
				 ->update([
					 'book_fee'=>$infoc['book_fee'], 
					 'data_fee'=>$infoc['data_fee'],
					 'travel_fee'=>$infoc['travel_fee'],
					 'meeting_fee'=>$infoc['meeting_fee'],
					 'international_fee'=>$infoc['international_fee'],
					 'equipment_fee'=>$infoc['equipment_fee'],
					 'labour_fee'=>$infoc['labour_fee'], 
					 'print_fee'=>$infoc['print_fee'],
					 'administration_fee'=>$infoc['administration_fee'],
					 'other_fee'=>$infoc['other_fee'],
					 'all_fee'=>$infoc['all_fee'],
					 'consulting_fee'=>$infoc['consulting_fee'],
				 ]);

	  $infob['unfund_id']=(int)$unfund_id;
	  $infob['exfund_id']=(int)$exfund_id;
		 $res3=Db::table('oa_report_project_mid')
			   ->where('id',$mid_id)
			   ->update([
				 'filling_date'=>date("Y-m-d"), 
				 'research_progress'=>$infob['research_progress'],
				 'funds'=>$infob['funds'],
				 'next_plan'=>$infob['next_plan'],
				 'funding_gap'=>$infob['funding_gap'],
				 'status'=>2,
			  ]);
	  $infod['mid_id']=(int)$mid_id;
	 
	  for($i=0;$i<count($param['achievement_form']);$i++)
	  {
		 
		 $infod['achievement_name']=$param['achievement_form'][$i]['achievement_name'];
		 $infod['achievement_author']=$param['achievement_form'][$i]['achievement_author'];
		 $infod['achievement_type']=$param['achievement_form'][$i]['achievement_type'];
		 $infod['achievement_publish']=$param['achievement_form'][$i]['achievement_publish'];
		 $infod['achievement_publish_time']=$param['achievement_form'][$i]['achievement_publish_time'];
		 $infod['achievement_number']=$param['achievement_form'][$i]['achievement_number'];
		 $infod['achievement']=$param['achievement_form'][$i]['achievement'];
		 if($param['achievement_form'][$i]['id']){
			 Db::table('oa_report_achievement')
				->where('id',$param['achievement_form'][$i]['id'])
				->update([
				 'achievement_name' =>$infod['achievement_name'],
				 'achievement_author' =>$infod['achievement_author'],
				 'achievement_type' =>$infod['achievement_type'],
				 'achievement_publish' =>$infod['achievement_publish'],
				 'achievement_publish_time' =>$infod['achievement_publish_time'],
				 'achievement_number' =>$infod['achievement_number'],
				 'achievement' =>$infod['achievement'],
				]);
		 }else{
			 Db::table('oa_report_achievement')
					 ->insert($infod);
		 }
	  }
	  Db::table('oa_report_subproject')
			   ->alias('s')
			   ->join('oa_report_project_mid p','p.subproject_id=s.id')
			   ->where('p.id',$mid_id)
			   ->update([
				   's.status'=>10,
			   ]);
	  return true;

 }
    //查看中期审核详情
    public function ListMid($param){
    	$subproject_id=$param['subproject_id'];
    	$train=Db::table('oa_report_subproject')
    	     ->alias('s')
    	     ->join('oa_admin_train t','t.id=s.train_id')
    	     ->where('s.id',$subproject_id)
    	     ->field('s.pro_no,s.finish_time,s.title,t.name,t.office,t.phone,s.postal_address,s.post_code,t.email,s.unit')
    	     ->select();
     	$project_mid=Db::table('oa_report_project_mid')
    	     ->where('subproject_id',$subproject_id)
    	     ->select();
    	$exfund_id=$project_mid[0]['exfund_id'];
    	$unfund_id=$project_mid[0]['unfund_id'];
    	$exfund=Db::table('oa_report_fund')
			 ->where('id',$exfund_id)
			 ->field('id,book_fee,data_fee,travel_fee,meeting_fee,international_fee,equipment_fee,consulting_fee,labour_fee,print_fee,administration_fee,other_fee,all_fee,surplus')
    	     ->select();
    	$unfund=Db::table('oa_report_fund')
			 ->where('id',$unfund_id)
			 ->field('id,book_fee,data_fee,travel_fee,meeting_fee,international_fee,equipment_fee,consulting_fee,labour_fee,print_fee,administration_fee,other_fee,all_fee')
			 ->select();
			 
		$achievement=Db::table('oa_report_achievement')
		->where('mid_id',$project_mid[0]['id'])
		->where('is_del','1')
		->select();
		$data['train']=$train;
		$data['achievementrain']=$achievement;
    	$data['project_mid']=$project_mid;
		$data['exfund']=$exfund;
		$data['exfund'][0]['exfund_id']=$exfund[0]['id'];
		$data['exfund'][0]['unfund_id']=$unfund[0]['id'];
		$data['exfund'][0]['book_fee_budget']=$unfund[0]['book_fee'];
		$data['exfund'][0]['data_fee_budget']=$unfund[0]['data_fee'];
		$data['exfund'][0]['travel_fee_budget']=$unfund[0]['travel_fee'];
		$data['exfund'][0]['meeting_fee_budget']=$unfund[0]['meeting_fee'];
		$data['exfund'][0]['international_fee_budget']=$unfund[0]['international_fee'];
		$data['exfund'][0]['equipment_fee_budget']=$unfund[0]['equipment_fee'];
		$data['exfund'][0]['labour_fee_budget']=$unfund[0]['labour_fee'];
		$data['exfund'][0]['print_fee_budget']=$unfund[0]['print_fee'];
		$data['exfund'][0]['administration_fee_budget']=$unfund[0]['administration_fee'];
		$data['exfund'][0]['other_fee_budget']=$unfund[0]['other_fee'];
		$data['exfund'][0]['all_fee_budget']=$unfund[0]['all_fee'];
		$data['exfund'][0]['consulting_fee_budget']=$unfund[0]['consulting_fee'];
		$data['train'][0]['filling_date']=$data['project_mid'][0]['filling_date'];
       return $data;
	}
	
	//项目结题新增
	public function WriteEnd($param)
	{	
		$infoa['book_fee']=$param['form']['book_fee'];
		$infoa['book_fee_basis']=$param['form']['book_fee_basis'];
		$infoa['data_fee']=$param['form']['data_fee'];
		$infoa['data_fee_basis']=$param['form']['data_fee_basis'];
		$infoa['travel_fee']=$param['form']['travel_fee'];
		$infoa['travel_fee_basis']=$param['form']['travel_fee_basis'];
		$infoa['meeting_fee']=$param['form']['meeting_fee'];
		$infoa['meeting_fee_basis']=$param['form']['meeting_fee_basis'];
		$infoa['international_fee']=$param['form']['international_fee'];
		$infoa['international_fee_basis']=$param['form']['international_fee_basis'];
		$infoa['equipment_fee']=$param['form']['equipment_fee'];
		$infoa['equipment_fee_basis']=$param['form']['equipment_fee_basis'];
		$infoa['consulting_fee']=$param['form']['consulting_fee'];
		$infoa['consulting_fee_basis']=$param['form']['consulting_fee_basis'];
		$infoa['labour_fee']=$param['form']['labour_fee'];
		$infoa['labour_fee_basis']=$param['form']['labour_fee_basis'];
		$infoa['print_fee']=$param['form']['print_fee'];
		$infoa['print_fee_basis']=$param['form']['print_fee_basis'];
		$infoa['administration_fee']=$param['form']['administration_fee'];
		$infoa['administration_fee_basis']=$param['form']['administration_fee_basis'];
		$infoa['other_fee']=$param['form']['other_fee'];
		$infoa['other_fee_basis']=$param['form']['other_fee_basis'];
		$infoa['status']=4;
		$fund_id=Db::table('oa_report_fund')
				->insertGetId($infoa);
		$info['fund_id']=(int)$fund_id;
		$info['subproject_id']=$param['form']['id'];
		$info['direct_date']=$param['form']['direct_date'];
		$info['final_result']=$param['final_result'][0];
		$info['real_date']=$param['form']['real_date'];
		$info['identify_date']=$param['form']['identify_date'];
		$info['total_report']=$param['total_report'];
		$info['agr_expense']=$param['form']['agr_expense'];
		$info['real_expense']=$param['form']['real_expense'];
		$info['mating_expense']=$param['form']['mating_expense'];
		$info['matreal_expense']=$param['form']['matreal_expense'];
		$info['provincial_fund']=$param['form']['provincial_fund'];
		$info['sch_fund']=$param['form']['sch_fund'];
		$info['personal_fund']=$param['form']['personal_fund'];
		$info['funds']=$param['form']['funds'];
		$info['sch_fund']=$param['form']['sch_fund'];
		$info['direct_date']=date("Y-m-d");
		$info['realtotal_expense']=$param['form']['realtotal_expense'];
		$project_end_id=Db::table('oa_report_project_end')
					   ->insertGetId($info);
		Db::table('oa_report_subproject')
		    ->where('id',$info['subproject_id'])
		    ->update([
				'status'=>16,
			]);   
		for($i=0;$i<count($param['achievement']);$i++)
		{		
			$infod['achievement_name']=$param['achievement'][$i]['achievement_name'];
			$infod['achievement_author']=$param['achievement'][$i]['achievement_author'];
			$infod['achievement_type']=$param['achievement'][$i]['achievement_type'];
			$infod['achievement_publish']=$param['achievement'][$i]['achievement_publish'];
			$infod['achievement_publish_time']=$param['achievement'][$i]['achievement_publish_time'];
			$infod['achievement_number']=$param['achievement'][$i]['achievement_number'];
			$infod['achievement']=$param['achievement'][$i]['achievement'];
			$infod['end_id']=(int)$project_end_id;
				Db::table('oa_report_achievement')
						->insert($infod);   				
		 }
		 return true;
	}

	//项目结题修改
	public function ReviseEnd($param)
	{	
		if(count($param['delete'])!=0){
			Db::table('oa_report_achievement')
			->where('id',"in",$param['delete'])
			->update([
			 'is_del'=>'2',
			]);
		}
		$fund_id=$param['formId']['fund_id'];
		$end_id=$param['formId']['end_id'];
		Db::table('oa_report_fund')
				->where('id',$fund_id)
				->update([
				'book_fee'=>$param['form']['book_fee'],
				'book_fee_basis'=>$param['form']['book_fee_basis'],
				'data_fee'=>$param['form']['data_fee'],
				'data_fee_basis'=>$param['form']['data_fee_basis'],	
				'travel_fee'=>$param['form']['travel_fee'],
				'travel_fee_basis'=>$param['form']['travel_fee_basis'],
				'meeting_fee'=>$param['form']['meeting_fee'],
				'meeting_fee_basis'=>$param['form']['meeting_fee_basis'],
				'international_fee'=>$param['form']['international_fee'],
				'international_fee_basis'=>$param['form']['international_fee_basis'],
				'equipment_fee'=>$param['form']['equipment_fee'],
				'equipment_fee_basis'=>$param['form']['equipment_fee_basis'],
				'consulting_fee'=>$param['form']['consulting_fee'],
				'consulting_fee_basis'=>$param['form']['consulting_fee_basis'],
				'labour_fee'=>$$param['form']['labour_fee'],
				'labour_fee_basis'=>$param['form']['labour_fee_basis'],
				'print_fee'=>$param['form']['print_fee'],
				'print_fee_basis'=>$param['form']['print_fee_basis'],
				'administration_fee'=>$param['form']['administration_fee'],
				'administration_fee_basis'=>$param['form']['administration_fee_basis'],
				'other_fee'=>$param['form']['other_fee'],
				'other_fee_basis'=>$param['form']['other_fee_basis'],
				]);
   

	Db::table('oa_report_project_end')
				->where('id',$end_id)
				->update([
				'final_result'=>$a,
				'real_date'=>$param['form']['real_date'],
				'identify_date'=>$param['form']['identify_date'],	
				'total_report'=>$param['total_report'],
				'agr_expense'=>$param['form']['agr_expense'],
				'real_expense'=>$param['form']['real_expense'],
				'mating_expense'=>$param['form']['mating_expense'],
				'matreal_expense'=>$param['form']['matreal_expense'],
				'provincial_fund'=>$param['form']['provincial_fund'],
				'sch_fund'=>$param['form']['sch_fund'],
				'personal_fund'=>$param['form']['personal_fund'],
				'funds'=>$param['form']['funds'],
				'realtotal_expense'=>$param['form']['realtotal_expense'],
				]);
		for($i=0;$i<count($param['achievement']);$i++)
		{		
			$infod['achievement_name']=$param['achievement'][$i]['achievement_name'];
			$infod['achievement_author']=$param['achievement'][$i]['achievement_author'];
			$infod['achievement_type']=$param['achievement'][$i]['achievement_type'];
			$infod['achievement_publish']=$param['achievement'][$i]['achievement_publish'];
			$infod['achievement_publish_time']=$param['achievement'][$i]['achievement_publish_time'];
			$infod['achievement_number']=$param['achievement'][$i]['achievement_number'];
			$infod['achievement']=$param['achievement'][$i]['achievement'];
			$infod['end_id']=$project_end_id;
			if($param['achievement_form'][$i]['id']){
				Db::table('oa_report_achievement')
					->where('id',$param['achievement_form'][$i]['id'])
						->update([
							'achievement_name'=>$param['achievement'][$i]['achievement_name'],
							'achievement_author'=>$param['achievement'][$i]['achievement_author'],
							'achievement_type'=>$param['achievement'][$i]['achievement_type'],
							'achievement_publish'=>$param['achievement'][$i]['achievement_publish'],
							'achievement_publish_time'=>$param['achievement'][$i]['achievement_publish_time'],
							'achievement_number'=>$param['achievement'][$i]['achievement_number'],
							'achievement'=>$param['achievement'][$i]['achievement'],
						]); 
			}else{
				Db::table('oa_report_achievement')
						->insert($infod);
			}  				
		 }
		 Db::table('oa_report_subproject')
		 ->alias('s')
		 ->join('oa_report_project_end p','p.subproject_id=s.id')
		 ->where('p.id',$end_id)
		 ->update([
			 's.status'=>16,
		 ]);
		 return  true;
	}
	//项目结题详情
	public function ListEnd($param){
		$subproject_id=$param['subproject_id'];
		$project=Db::table('oa_report_subproject')
				 ->alias('s')
				 ->join('oa_report_project p','p.id=s.project_id')
				 ->join('oa_admin_train t','t.id=s.train_id')
				 ->where('s.id',$subproject_id)
				 ->field('p.parent,p.children,p.year,s.title,s.theme,s.pro_no,s.research_direction,s.finish_time,s.result,t.name,t.sex,t.nation,t.birth,t.sch,t.preoccupation,t.majoroccupation,t.edu,t.degree,t.address,t.postalcode,t.phone,t.email')
				 ->find();
		$partner=Db::table('oa_report_partner')
				->where('subproject_id',$subproject_id)
				->select();
		$project_end=Db::table('oa_report_project_end')
					->where('subproject_id',$subproject_id)
					->find();
		$fund_id=$project_end['fund_id'];
		$end_id=$project_end['id'];
		$mid_id1=Db::table('oa_report_project_mid')
			   ->where('subproject_id',$subproject_id)
			   ->field('id')
			   ->find();
		$mid_id=$mid_id1['id'];
		$fund=Db::table('oa_report_fund')
			 ->where('id',$fund_id)
			 ->find();
		$achievement=Db::table('oa_report_achievement')
					->where('end_id',$end_id)
				    ->whereOr('mid_id',$mid_id)
					->select();
		$people_name=$project['name'];
		for($i=0;$i<count($partner);$i++){
			$people_name=$people_name.','.$partner[$i]['name'];
		}
		$data['people_name']=$people_name;
		$data['project']=$project;
		$data['partner']=$partner;
		$data['project_end']=$project_end;
		$data['fund']=$fund;
		$data['project']=$project;
		$data['achievement']=$achievement;
		return $data;
	}

	//结题一览表
	public function ListProjectend($param)
	{  
		$list=Db::table('oa_admin_train')
				->alias('t')
				->join('oa_report_subproject s','s.train_id=t.id')
				->join('oa_report_project p','p.id=s.project_id')
				->where('s.status',21)
				->field('t.name,t.sch,p.parent,p.children,s.title,s.pro_no,s.support,s.id')
				->select();
		for($i=0;$i<count($list);$i++)
		{
			$subproject_id=$list[$i]['id'];
			$evaluate=Db::table('oa_report_review_end')
					 ->alias('r')
					 ->join('oa_report_expert_end e','e.id=r.expert_id')
					 ->where('r.subproject_id',$subproject_id)
					 ->field('r.evaluate,e.name')
					 ->select();
			$a=0;
			for($j=0;$j<count($evaluate);$j++)
			{	
				$a=$a+$evaluate[$j]['evaluate'];
			}
			$average=$a/$j;
			$list[$i]['evaluate']=$evaluate;
			$list[$i]['average']=$average;
		}	
		 return $list;
	}

   //----------------教育厅---------------------
   


   //发布项目
   public function WriteEdu($param)
  {
  	$info['parent']=$param['parent'];
  	$info['children']=$param['children'];
  	$info['year']=$param['year'];
  	$info['start_time']=$param['start_time'];
  	$info['end_time']=$param['end_time'];
  	$info['status']=1;
  	$res=Db::table('oa_report_project')
  	     ->insert($info);
  	if($res){
  		return true;
  	}else{
  		return false;
  	}
  }

  //修改项目
  public function ReviseEdu($param)
  {
  	$id=$param['id'];
  	$info['parent']=$param['parent'];
  	$info['children']=$param['children'];
  	$info['year']=$param['year'];
  	$info['start_time']=$param['start_time'];
  	$info['end_time']=$param['end_time'];
  	$res=Db::table('oa_report_project')
  	     ->where('id',$id)
  	     ->update([
  	     	'parent'=> $info['parent'],
  	     	'children'=> $info['children'],
  	     	'year'=> $info['year'],
  	     	'start_time'=> $info['start_time'],
  	     	'end_time'=> $info['end_time'],
  	     	]);
  	if($res){
  		return true;
  	}else{
  		return false;
  	}
  }
  //删除项目
   public function DeleteEdu($param){
	   $project_id=$param['id'];
	   $res=Db::table('oa_report_project')
	        ->where('id',$project_id)
	        ->update([
				'is_del'=>'2',
			]);
	   if($res){
	   	return true;
	   }else{
	   	return false;
	   }
   }

  //查看项目(三种权限都用这个)
   public function ListEdu($param)
  { 
  	$map=[];
  	$parent=$param['parent'];
    $children=$param['children'];
    $year=$param['year'];
	if(isset($parent)&&$parent!='')
	{
		$map['parent']=$param['parent'];
	}
	if(isset($children)&&$children!='')
	{
		$map['children']=$param['children'];
	}
	if(isset($year)&&$year!='')
	{
		$map['year']=$param['year'];
	}
  	$list=Db::table('oa_report_project')
  	      ->where($map)
  	      ->paginate(config('pagesize'), false, ['page' => $page]);
  	return $list;
  }
  //教育厅查看专家
  public function ListExpert($param)
  {   $level=(int)$param['level'];
		if($level==4){
			$a='oa_report_expert';
		}else if($level==5){
			$a='oa_report_expert_end';
		}	  
	$expert=Db::table($a)
			->alias('e')
			->join('oa_admin_user u','u.id=e.user_id')
			->where('e.project_id',$param['project_id'])
			->order('id desc')
			->field('e.id,e.name,e.unit,e.direction,e.jobrank,e.idnumber,u.username,e.password')
			->paginate(config('pagesize'), false, ['page' => $page]);
  	return $expert;
  }
  //教育厅查看分组
  public function ListGroup($param)
  {
    $group=Db::table('oa_report_group')
            ->where('project_id', $param['project_id'])
            ->order('id desc')
  	        ->paginate(config('pagesize'), false, ['page' => $param['page']]);
  	return $group;
  }
    //结题所有分组
	public function endGroupList($param)
	{
	  $group=Db::table('oa_report_group_end')
			  ->where('project_id', $param['project_id'])
			  ->order('id desc')
				->paginate(config('pagesize'), false, ['page' => $param['page']]);
		return $group;
	}
  //添加分组
   public function WriteGroup($param)
  {
  	$info['project_id']=$param['project_id'];
  	$info['title']=$param['title'];
  	$info['no']=$param['no'];
  	$res=Db::table('oa_report_group')
  	     ->insert($info);
  	if($res){
  		return true;
  	}else{
  		return false;
  	}
  }
    //结题添加分组
	public function endGroupWrite($param) 
	{
	  $info['project_id']=$param['project_id'];
		$info['title']=$param['title'];
		$info['no']=$param['no'];
		$res=Db::table('oa_report_group_end')
			 ->insert($info);
		if($res){
			return true;
		}else{
			return false;
		}
	}
  //修改分组信息
   public function ReviseGroup($param)
  {
  	$id=$param['id'];
  	$info['project_id']=$param['project_id'];
  	$info['title']=$param['title'];
  	$info['no']=$param['no'];
  	$res=Db::table('oa_report_group')
  	     ->where('id',$id)
	     ->update([
  	     	'project_id'=> $info['project_id'],
  	     	'title'=> $info['title'],
  	     	'no'=> $info['no'],
  	     	]);
  	if($res){
  		return true;
  	}else{
  		return false;
  	}
  }
  //删除分组信息
     public function DeleteGroup($param)
    {
	   $id=$param['id'];
	   $res=Db::table('oa_report_group')
	        ->where('id',$id)
	        ->delete();
	   if($res){
	   	return true;
	   }else{
	   	return false;
	   }
   }
  //教育厅导入立项专家
  public function ImportExpert($data){
	
	$param=$data['tableData'];
  $project_id=(int)$data['project_id'];
  $type=(int)$data['type'];
	   $error=[];
	   for($i=0;$i<count($param);$i++){
		   $res1=Db::table('oa_report_subproject')
					   ->alias('s')
					   ->join('oa_admin_train t','t.id=s.train_id')
					   ->where('s.status','<>',22)
					   ->where('s.is_del','1')
					   ->where('t.idnumber',$param[$i]['idnumber'])
					   ->find();
		   $res2=Db::table('oa_report_subproject')
				   ->alias('s')
				   ->join('oa_report_partner p','p.subproject_id=s.id')
				   ->where('s.status','<>',22)
				   ->where('s.is_del','1')
				   ->where('p.idnumber',$param[$i]['idnumber'])
		 		   ->find();
		 if($type==1){
		   $res3=Db::table('oa_report_expert')
		   ->where('idnumber',$param[$i]['idnumber'])
		   ->where('project_id',$project_id)
		   ->find();
		 }else if($type==3){
		   $res3=Db::table('oa_report_expert_end')
		   ->where('idnumber',$param[$i]['idnumber'])
		   ->where('project_id',$project_id)
		   ->find();
		 }
		   if($res1||$res2){
			   array_push($error, ['index' => $param[$i]['index'], 'msg' => '申报此类项目的主要负责人或主要参与者不能成为专家']);
		   }else if($res3){
			   array_push($error, ['index' => $param[$i]['index'], 'msg' => '专家已存在此项目中']);
		   }else{
			   $info['name']=$param[$i]['name'];
			   $info['idnumber']=$param[$i]['idnumber'];
			   $info['direction']=$param[$i]['direction'];
			   $info['jobrank']=$param[$i]['jobrank'];
			   $info['unit']=$param[$i]['unit'];
			   $info['project_id']=$project_id;
			   $infoa['username']=$this->create_password(8);
			   $password=$this->create_password(8);
			   $infoa['password']=user_md5($password);
			   $infoa['create_time']=time();
			   $infoa['realname']=$param[$i]['name'];
	   $infoa['status']=1;
	   if($type==1){
		 $infoa['level']=4;
	   }else if($type==3){
		 $infoa['level']=5;
	   }
			   $user_id=Db::table('oa_admin_user')
					   ->insertGetId($infoa);
			   $infob['user_id']=$user_id;
			   if($type==1){
				$infob['group_id']=20;
			  }else if($type==3){
				$infob['group_id']=21;
			  }
			   Db::table('oa_admin_access')
			   ->insert($infob);
			   $info['user_id']=$user_id;
	   $info['status']=2;
	   $info['password']=$password;
	   if($type==1){
		 Db::table('oa_report_expert')
		 ->insert($info);		
	   }else if($type==3){
		 Db::table('oa_report_expert_end')
		 ->insert($info);	
	   }			
		   }
	   }
	   if (count($error) > 0) {
		   return ['isSuccess' => false, 'errorList' => $error];
		 }
		 return ['isSuccess' => true];
  }
  //教育厅分配材料与专家
   public function DistributionEdu($param)
  {
  	$group_id=$param['group_id'];
  	$expert_id=$param['expert_id'];
  	$subproject_id=$param['subproject_id'];
  		$res1=Db::table('oa_report_expert')
  		     ->where('id',"in",$expert_id)
  		     ->update([
  		     	'group_id'=>$group_id,
  		     	]);

  		$res2=Db::table('oa_report_subproject')
  		     ->where('id',"in",$subproject_id)
  		     ->update([
  		     	'group_id'=>$group_id,
  		     	]);
  		for($i=0;$i<count($expert_id);$i++){
  			for($j=0;$j<count($subproject_id);$j++)
  			{
  				$info['expert_id']=$expert_id[$i];
  				$info['subproject_id']=$subproject_id[$j];
  				$info['status']=1;
  				Db::table('oa_report_review')
  				   ->insert($info);
  			}
  		}
  	    return true;

  }

  //审核
  public function Check($param)
  {

  	$subproject_id=$param['subproject_id'];
	$status=$param['status'];
	if($status==10){
		Db::table('oa_report_subproject')
			->where('id',$subproject_id)
  		     ->update([
				   'support'=>$param['support'],
				   ]); 
		$infoa['status']=2;
		$infob['status']=3;
		$exfund=Db::table('oa_report_fund')
			   ->insertGetId($infoa);
		$unfund=Db::table('oa_report_fund')
			   ->insertGetId($infob);
		$info['subproject_id']=$subproject_id;
		$info['status']=1;
		$info['exfund_id']=(int)$exfund;
		$info['unfund_id']=(int)$unfund;
		Db::table('oa_report_project_mid')
			->insert($info);
	}

  	$res=Db::table('oa_report_subproject')
  		     ->where('id',$subproject_id)
  		     ->update([
  		     	'status'=>$param['status'],
  		     	]); 
    if($res){
    	return true;
    }else{
    	return false;
    }
  }
  //教育厅结束项目
  public function EndEdu($param)
  {
	  $project_id=$param['project_id'];
	  $res=Db::table('oa_report_project')
		   ->where('id',$project_id)
		   ->update([
			'status'=>2,
		   ]);
	   if($res){
		   return true;
	   }else{
		   return false;
	   }
  }



  //--------------专家--------------
  
  //专家评审列表
  public function ReviewExpert($param)
  {
	  $user_id=(int)$param['id'];
	  $res=Db::table('oa_report_expert')
				 ->where('user_id',$user_id)
				 ->field('id, group_id')
				 ->find();
		$expert_id = $res['id'];
    $group_id = $res['group_id'];
    $list = [];
    if ($group_id != 0) {
      $list = Db::table('oa_report_subproject')
          ->alias('s')
          ->join('oa_report_review r', 's.id = r.subproject_id and r.expert_id = ' . $expert_id, 'left')
          ->join('oa_admin_train t','t.id = s.train_id')
          ->where('s.group_id', $group_id)
          // ->where('r.expert_id', $expert_id)
          ->where('s.status', 'in', [6, 7, 8])
          ->field('s.id as subproject_id, '. $expert_id .' as expert_id, r.id as review_id ,s.title,t.name,t.jobrank, t.sch, r.status')
          ->paginate(config('pagesize'), false, ['page' => $page]);
    }
	  // $list=Db::table('oa_report_review')
		// 	  ->alias('r')
		// 	  ->join('oa_report_subproject s','s.id=r.subproject_id')
		// 	  ->join('oa_admin_train t','t.id=s.train_id')
    //     ->where('r.expert_id',$expert_id)
    //     ->where('s.status', 'in', [6, 7, 8])
		//       ->field('r.id,r.subproject_id,s.title,t.name,t.jobrank,t.sch,r.status')
  	//           ->paginate(config('pagesize'), false, ['page' => $page]);
  	return $list;
  }
    //专家评审列表(结题)
	public function endExpertReview($param)
	{
		$user_id=(int)$param['id'];
		$res=Db::table('oa_report_expert_end')
				   ->where('user_id',$user_id)
				   ->field('id, group_id')
				   ->find();
		  $expert_id = $res['id'];
	  $group_id = $res['group_id'];
	  $list = [];
	  if ($group_id != 0) {
		$list = Db::table('oa_report_subproject')
			->alias('s')
			->join('oa_report_review_end r', 's.id = r.subproject_id and r.expert_id = ' . $expert_id, 'left')
			->join('oa_admin_train t','t.id = s.train_id')
			->where('s.groupend_id', $group_id)
			// ->where('r.expert_id', $expert_id)
			->where('s.status', 'in', [20, 21])
			->field('s.id as subproject_id, '. $expert_id .' as expert_id, r.id as review_id ,s.title,t.name,t.jobrank, t.sch, r.status')
			->paginate(config('pagesize'), false, ['page' => $page]);
	  }
		// $list=Db::table('oa_report_review')
		  // 	  ->alias('r')
		  // 	  ->join('oa_report_subproject s','s.id=r.subproject_id')
		  // 	  ->join('oa_admin_train t','t.id=s.train_id')
	  //     ->where('r.expert_id',$expert_id)
	  //     ->where('s.status', 'in', [6, 7, 8])
		  //       ->field('r.id,r.subproject_id,s.title,t.name,t.jobrank,t.sch,r.status')
		//           ->paginate(config('pagesize'), false, ['page' => $page]);
		return $list;
	}

  //专家立项评审
  public function WriteExpert($param)
  {
    $id=$param['id'];
    $user_id = $param['user_id'];
	  $info['expert_id']=$param['expert_id'];
	  $info['subproject_id']=$param['subproject_id'];
  	$info['score1']=$param['score1'];
  	$info['score2']=$param['score1'];
  	$info['score3']=$param['score1'];
    $info['remark']=$param['remark'];
    $info['evaluate']=$param['evaluate'];
    $info['status']=2;
    if ($id == null) {
      // 没有数据要增加
      $res=Db::table('oa_report_review')
          ->insert($info);
    } else {
      $res=Db::table('oa_report_review')
          ->where('id',$id)
          ->update($info);
    }
    // 判断当前项目是否全部评审完成
    $expert = Db::table('oa_report_expert')->where('user_id', $user_id)->find();
    $experts = Db::table('oa_report_expert')->where('group_id', $expert['group_id'])->select();
    $expert_ids = [];
    foreach ($experts as $key => $value) {
      array_push($expert_ids, $value['id']);
    }
    if (count($expert_ids) > 0) {
      // 找出所有评审记录
      $res1 = Db::table('oa_report_review')
            ->where('subproject_id', $param['subproject_id'])
            ->where('expert_id', 'in', $expert_ids)
            ->where('status', '2')
            ->select();
      if (count($res1) == count($expert_ids)) {
        // 将当前项目置为评审完成
		Db::table('oa_report_subproject')->where('id', $param['subproject_id'])->update(['status' => 8]);
		// 将当前组设置为不可分配
        Db::table('oa_report_group')->where('id', $expert['group_id'])->update(['status' => 2]);
      }
    }
    // 判断是否该专家完成了所有的项目评审
    // 获取所有材料
    $subprojects = Db::table('oa_report_subproject')->where('group_id', $expert['group_id'])->select();
    $subproject_ids = [];
    foreach ($subprojects as $key => $value) {
      array_push($subproject_ids, $value['id']);
    }
    // 找出所有的评审结果
    if (count($subproject_ids) > 0) {
      $res2 = Db::table('oa_report_review')
            ->where('subproject_id', 'in', $subproject_ids)
            ->where('expert_id', $param['expert_id'])
            ->where('status', '2')
            ->select();
      if (count($res2) == count($subproject_ids)) {
        // 当前用户已经完成该项目的所有子项目评审
		Db::table('oa_admin_user')->where('id', $user_id)->update(['status' => 0]);
		// 将当前组设置为不可分配
		Db::table('oa_report_group')->where('id', $expert['group_id'])->update(['status' => 2]);
      }
    }
	if($res){
	  return true;
	}else{
		return false;
	}

  }

   //专家评审(结题)
   public function endExpertWrite($param)
   {
	 $id=$param['id'];
	 $user_id = $param['user_id'];
	   $info['expert_id']=$param['expert_id'];
	   $info['subproject_id']=$param['subproject_id'];
	   $info['score1']=$param['score1'];
	   $info['score2']=$param['score2'];
	   $info['score3']=$param['score3'];
	 $info['remark']=$param['remark'];
	 $info['evaluate']=$param['evaluate'];
	 $info['status']=2;
	 if ($id == null) {
	   // 没有数据要增加
	   $res=Db::table('oa_report_review_end')
		   ->insert($info);
	 } else {
	   $res=Db::table('oa_report_review_end')
		   ->where('id',$id)
		   ->update($info);
	 }
	 // 判断当前项目是否全部评审完成
	 $expert = Db::table('oa_report_expert_end')->where('user_id', $user_id)->find();
	 $experts = Db::table('oa_report_expert_end')->where('group_id', $expert['group_id'])->select();
	 $expert_ids = [];
	 foreach ($experts as $key => $value) {
	   array_push($expert_ids, $value['id']);
	 }
	 if (count($expert_ids) > 0) {
	   // 找出所有评审记录
	   $res1 = Db::table('oa_report_review_end')
			 ->where('subproject_id', $param['subproject_id'])
			 ->where('expert_id', 'in', $expert_ids)
			 ->where('status', '2')
			 ->select();
	   if (count($res1) == count($expert_ids)) {
		 // 将当前项目置为评审完成
		 Db::table('oa_report_subproject')->where('id', $param['subproject_id'])->update(['status' => 21]);
		 // 将当前组设置为不可分配
		 Db::table('oa_report_group_end')->where('id', $expert['group_id'])->update(['status' => 2]);
	   }
	 }
	 // 判断是否该专家完成了所有的项目评审
	 // 获取所有材料
	 $subprojects = Db::table('oa_report_subproject')->where('groupend_id', $expert['group_id'])->select();
	 $subproject_ids = [];
	 foreach ($subprojects as $key => $value) {
	   array_push($subproject_ids, $value['id']);
	 }
	 // 找出所有的评审结果
	 if (count($subproject_ids) > 0) {
	   $res2 = Db::table('oa_report_review_end')
			 ->where('subproject_id', 'in', $subproject_ids)
			 ->where('expert_id', $param['expert_id'])
			 ->where('status', '2')
			 ->select();
	   if (count($res2) == count($subproject_ids)) {
		 // 当前用户已经完成该项目的所有子项目评审
		 Db::table('oa_admin_user')->where('id', $user_id)->update(['status' => 0]);
		 // 将当前组设置为不可分配
		 Db::table('oa_report_group_end')->where('id', $expert['group_id'])->update(['status' => 2]);
	   }
	 }
	 if($res){
	   return true;
	 }else{
	   return false;
	 }
   }
  //专家立项评审结果
  public function ResultExpert($param)
  {
	  $subproject_id=$param['subproject_id'];
	  $data=Db::table('oa_report_review')
		   ->alias('r')
		   ->join('oa_report_expert e','e.id=r.expert_id')
		   ->where('r.subproject_id',$subproject_id)
		   ->where('r.status',2)
		   ->field('r.id,e.name,r.score1,r.score2,r.score3,r.evaluate,r.remark')
		   ->select();
	  return $data;
  }

    // 获取指定大项目所有未分配专家
	public function getAllExpert($project_id) 
	{
	  $where = [
		'e.project_id' => $project_id,
		'e.status' => 2];
	  $data = Db::table('oa_report_expert')
			  ->alias('e')
			  ->join('oa_report_review r', 'e.id = r.expert_id', 'left')
			  ->group('e.id')
			  ->where($where)
			  ->field("e.id,e.project_id,e.name,e.idnumber,e.direction,e.jobrank,e.unit,e.group_id,e.status,  count(r.id) as count")
			  ->select();
	  return $data ? $data : [];
	  // , count(r.subproject_id) as count
	}
    // 获取指定大项目所有专家(结项)
	public function getEndAllExpert($project_id)
	{
	  $where = [
		'e.project_id' => $project_id,
		'e.status' => 2];
		$data = Db::table('oa_report_expert_end')
			->alias('e')
			->join('oa_report_review_end r', 'e.id = r.expert_id', 'left')
			->group('e.id')
			->where($where)
			->field("e.id,e.project_id,e.name,e.idnumber,e.direction,e.jobrank,e.unit,e.group_id,e.status,  count(r.id) as count")
			->select();
		return $data ? $data : [];
	}

	// 获取指定大项目所有材料
	public function getAllMaterial($project_id) 
	{
	  $data = Db::table('oa_report_subproject')
			  ->alias('s')
			  ->join('oa_report_review r', 's.id = r.subproject_id', 'left')
			  ->where('s.project_id', $project_id)
			  ->where('s.status', 'in', [2,4,6,8,10])
			  ->where('s.is_del', 1)
			  ->group('s.id')
			  ->field('s.id,s.project_id,s.train_id,s.title,s.theme,s.research_direction,s.unit,s.result,s.word_count,s.pro_no,s.status,s.finish_time,s.achievement,s.post_code,s.postal_address,s.is_del,s.group_id,s.fund_id,s.support, count(r.id) as count')
			  ->select();
	  return $data ? $data : [];
	}
	  // 获取指定大项目所有材料(结项)
	  public function getEndAllMaterial($project_id) 
	  {
		$data = Db::table('oa_report_subproject')
				->alias('s')
				->join('oa_report_review_end r', 's.id = r.subproject_id', 'left')
				->where('s.project_id', $project_id)
				->where('s.status', 'in', [20, 21, 22, 23])
				->where('s.is_del', 1)
				->group('s.id')
				->field('s.id,s.project_id,s.train_id,s.title,s.theme,s.research_direction,s.unit,s.result,s.word_count,s.pro_no,s.status,s.finish_time,s.achievement,s.post_code,s.postal_address,s.is_del,s.group_id,s.groupend_id,s.fund_id,s.support, count(r.id) as count')
				->select();
		return $data ? $data : [];
	  }
	// 专家和材料分组
	public function setExtAndMat($group_id, $experts, $materials) 
	{
	// 为了防止多个教育厅同时操作后，产生脏读的问题，每次分组之前都需要判断
	// 如果该组内重新分配的材料已经被分配到了其他组，或者该材料已经被评审了，那么直接告诉他刷新页面
	// 如果该组内重新分配的专家已经被分配到了其他组，或者该专家已经做出评审了，那么直接告诉他刷新页面

	// 该材料状态正常，被分配到了其他组 => 待分配的材料只能在未分配和在当前组
	$materials_ = [];
	$experts_ = [];
	if (count($materials) > 0) {
		$errorArr = Db::table('oa_report_subproject')
				->where('status', 'in', [2,4,6,8,10])
				->where('id', 'in', $materials)
				->where('group_id', 'in', [0, $group_id])
				->where('is_del', 1)
				->select();
		if (count($errorArr) != count($materials)) {
		$this->error = '部分材料已经被分配了, 请重新分配';
		return false;
		}
		foreach ($errorArr as $key => $value) {
		if ($value['group_id'] == 0) {
			array_push($materials_, $value['id']);
		}
		}
	}
	// 带分配的不属于改组的材料已经被评审了，
	if (count($materials_) > 0) {
		$errorArr = Db::table('oa_report_review')
				->where('subproject_id', 'in', $materials_)
				->select();
		if (count($errorArr) > 0) {
		$this->error = '部分材料已经被评审了, 请重新分配';
		return false;
		}
	}
	// 该专家状态正常，被分配到其他组 => 待分配的专家只能在未分配和在当前组
	if (count($experts) > 0) {
		$errorArr = Db::table('oa_report_expert')
				->where('status', 2)
				->where('id', 'in', $experts)
				->where('group_id', 'in', [0, $group_id])
				->select();
		if (count($errorArr) != count($experts)) {
		$this->error = '部分专家已经被分配了, 请重新分配';
		return false;
		}
		foreach ($errorArr as $key => $value) {
		if ($value['group_id'] == 0) {
			array_push($experts_, $value['id']);
		}
		}
	}
	// 该专家已经做出评审了，
	if (count($experts_) > 0) {
		$errorArr = Db::table('oa_report_review')
				->where('expert_id', 'in', $experts_)
				->select();
		if (count($errorArr) > 0) {
		$this->error = '部分专家做出评审了, 请重新分配';
		return false;
		}
	}
	// 重新分配材料分两步，将所有属于改组的材料变为未分配， 在重新分配
	Db::table('oa_report_subproject')->where('group_id', $group_id)->update(['group_id' => 0]);
	if (count($materials) > 0) {
		Db::table('oa_report_subproject')->where('id', 'in', $materials)->update(['group_id' => $group_id]);
	}
	// 重新分配专家分两步，将所有属于改组的专家变为未分配， 在重新分配
	Db::table('oa_report_expert')->where('group_id', $group_id)->update(['group_id' => 0]);
	if (count($experts) > 0) {
		Db::table('oa_report_expert')->where('id', 'in', $experts)->update(['group_id' => $group_id]);
	}
	return true;
	}
	  // 专家和材料分组(结题)
	  public function setEndExtAndMat($group_id, $experts, $materials)
	  {
		// 为了防止多个教育厅同时操作后，产生脏读的问题，每次分组之前都需要判断
		// 如果该组内重新分配的材料已经被分配到了其他组，或者该材料已经被评审了，那么直接告诉他刷新页面
		// 如果该组内重新分配的专家已经被分配到了其他组，或者该专家已经做出评审了，那么直接告诉他刷新页面
	
		// 该材料状态正常，被分配到了其他组 => 待分配的材料只能在未分配和在当前组
		$materials_ = [];
		$experts_ = [];
		if (count($materials) > 0) {
		  $errorArr = Db::table('oa_report_subproject')
			  ->where('status', 'in', [20])
			  ->where('id', 'in', $materials)
			  ->where('groupend_id', 'in', [0, $group_id])
			  ->where('is_del', 1)
			  ->select();
		  if (count($errorArr) != count($materials)) {
			$this->error = '部分材料已经被分配了, 请重新分配';
			return false;
		  }
		  foreach ($errorArr as $key => $value) {
			if ($value['groupend_id'] == 0) {
			  array_push($materials_, $value['id']);
			}
		  }
		}
		// 带分配的不属于改组的材料已经被评审了，
		if (count($materials_) > 0) {
		  $errorArr = Db::table('oa_report_review_end')
			  ->where('subproject_id', 'in', $materials_)
			  ->select();
		  if (count($errorArr) > 0) {
		  $this->error = '部分材料已经被评审了, 请重新分配';
		  return false;
		  }
		}
		// 该专家状态正常，被分配到其他组 => 待分配的专家只能在未分配和在当前组
		if (count($experts) > 0) {
		  $errorArr = Db::table('oa_report_expert_end')
			  ->where('status', 2)
			  ->where('id', 'in', $experts)
			  ->where('group_id', 'in', [0, $group_id])
			  ->select();
		  if (count($errorArr) != count($experts)) {
		  $this->error = '部分专家已经被分配了, 请重新分配';
		  return false;
		  }
		  foreach ($errorArr as $key => $value) {
		  if ($value['group_id'] == 0) {
			array_push($experts_, $value['id']);
		  }
		  }
		}
		// 该专家已经做出评审了，
		if (count($experts_) > 0) {
		  $errorArr = Db::table('oa_report_review_end')
			  ->where('expert_id', 'in', $experts_)
			  ->select();
		  if (count($errorArr) > 0) {
		  $this->error = '部分专家做出评审了, 请重新分配';
		  return false;
		  }
		}
		// 重新分配材料分两步，将所有属于改组的材料变为未分配， 在重新分配
		Db::table('oa_report_subproject')->where('groupend_id', $group_id)->update(['groupend_id' => 0]);
		if (count($materials) > 0) {
		  Db::table('oa_report_subproject')->where('id', 'in', $materials)->update(['groupend_id' => $group_id]);
		}
		// 重新分配专家分两步，将所有属于改组的专家变为未分配， 在重新分配
		Db::table('oa_report_expert_end')->where('group_id', $group_id)->update(['group_id' => 0]);
		if (count($experts) > 0) {
		  Db::table('oa_report_expert_end')->where('id', 'in', $experts)->update(['group_id' => $group_id]);
		}
		return true;
	  }

   //判断主要负责人是否还有多的项目
	function istrain($train_id)
	{
		//判断主要参与者个人信息是否审核完成，是否有资格参加项目申报
		$res=Db::table('oa_admin_train')
		->where('id',$train_id)
		->where('status',2)
		->where('status1',2)
		->find();
		if(!($res)){
		   $error = '您的个人信息还未审核成功，无法申请项目';
	 	   $data['code']=false;
	 	   $data['error']=$error;
           return $data;
		}
        //判断主要参与者是否还有项目，切未结题
	 	$result1=Db::table('oa_report_subproject')
	 	         ->where('train_id',$train_id)
	 	         ->where('status','<>',22)
	 	         ->find();
	 	if($result1){
	 	   $error = '您有一个项目未结题，请勿申请项目';
	 	   $data['code']=false;
	 	   $data['error']=$error;
           return $data;
	 	}
	 	//判断已经是主要参与者了还参加项目
	 	$train=Db::table('oa_admin_train')
	 	        ->where('id',$train_id)
	 	        ->field('name,idnumber')
	 	        ->select();
	 	$idnumber=$train[0]['idnumber'];
	 	$name=$train[0]['name'];
	 	$subproject=Db::table('oa_report_partner')
	 	        ->where('idnumber',$idnumber)
	 	        ->where('name',$name)
	 	        ->field('subproject_id')
	 	        ->find();
 		$result2=Db::table('oa_report_subproject')
		     ->where('id',$subproject['subproject_id'])
		     ->where('status','<>',22)
		     ->find();
	  	if($result2){
	 	  $error = '您已经以参与者的身份参与了一个项目且已经立项，请勿再申请项目';
	 	   $data['code']=false;
	 	   $data['error']=$error;
           return $data;
	 	}
	 	 $data['code']=true;
         return $data;

	}
	//判断参与者是否还有多的项目
	function ispartner($partner)
	{
        for($i=0;$i<count($partner);$i++)
		{
			//判断参与者是否申请了项目
			$train_id=Db::table('oa_admin_train')
			          ->where('idnumber',$partner[$i]['idnumber'])
			          ->where('name',$partner[$i]['name'])
			          ->field('id,name')
			          ->select();

			$result1=Db::table('oa_report_subproject')
	 	         ->where('train_id',$train_id[0]['id'])
	 	         ->where('status','<>',22)
	 	         ->find(); 
			 	if($result1){
			 	   $error = '您的参与者:'.$train_id[0]['name'].' 有一个项目未结题，请勿再申请项目';

				   $data['code']=false;
			 	   $data['error']=$error;
		           return $data;
			 	}
	    //判断参与者是否参加了别的项目的参与
		 	$subproject=Db::table('oa_report_partner')
		 	        ->where('idnumber',$partner[$i]['idnumber'])
		 	        ->where('name',$partner[$i]['name'])
		 	        ->field('subproject_id')
		 	        ->find();
	 		$result2=Db::table('oa_report_subproject')
			     ->where('id',$subproject['subproject_id'])
			     ->where('status','<>',22)
			     ->find();
		  	if($result2){
		 	   $error='您填的参与者:'.$partner[$i]['name'].' 已经以参与者的身份参与了另一个项目且已经立项，请勿再申请项目';
		       $data['code']=false;
		 	   $data['error']=$error;
	           return $data;
		 	}
		 
        }
        $data['code']=true;
         return $data;
	}
	function create_password($pw_length = 4){
		$randpwd = '';
		for ($i = 0; $i <2; $i++){
			$randpwd .= chr(mt_rand(65, 90));
		}
		for ($i = 2; $i <$pw_length; $i++){
			$randpwd .= chr(mt_rand(48, 57));
		}
		return $randpwd;
	}
}